package com.lanlian.cayman.socket;

import android.widget.Toast;

import com.lanlian.cayman.utils.NetWorkUtil;
import com.lanlian.commonlib.base.BaseActivity;
import com.lanlian.commonlib.base.BaseFragment;

import java.net.SocketTimeoutException;

public class SocketCallImpl implements SocketCall {

    BaseActivity act;
    BaseFragment fragment;

    public SocketCallImpl(BaseActivity act) {
        this.act = act;
    }

    public SocketCallImpl(BaseFragment act) {
        this.fragment = act;
    }

    @Override
    public void init() {

    }

    @Override
    public void connect(boolean isConnet) {

    }

    @Override
    public void message(byte[] data) {

    }

    @Override
    public void close() {
        if (act != null) {
            act.dismissLoadingDialog();
        }
        if (fragment != null) {
            fragment.dismissLoadingDialog();
        }
    }

    @Override
    public void error(Exception e) {
        if (act != null) {
            act.dismissLoadingDialog();

        }
        if (fragment != null) {
            fragment.dismissLoadingDialog();

        }
        if(act==null)
        {
            return;
        }
        if(NetWorkUtil.isNetworkConnected(act))
        {
            if(e instanceof SocketTimeoutException)
            {
                Toast.makeText(act,"连接超时",Toast.LENGTH_LONG).show();
                return;
            }
            Toast.makeText(act,"服务连接异常",Toast.LENGTH_LONG).show();
        }
        else
        {
            Toast.makeText(act,"网络异常，请检查网络",Toast.LENGTH_LONG).show();
        }
    }
}
